<template>
  <div>

    <div class="mt-[32px]">
      <el-form label-width="85px" :rules="rules" :model="form" ref="FormRef">
        <el-form-item :label="item.name" :prop="item.code"
                      v-for="(item,index) in config"
                      :key="index"
        >

          <el-input v-model="form[item.code]"
                    v-if="!item.type"
          />
          <el-input v-model="form[item.code]"
                    v-if="item.type == 'number'"
                    type="number"
          />

          <el-input v-model="form[item.code]"
                    type="textarea"
                    :rows="5"
                    v-if="item.type == 'textarea'"
          />

          <el-select v-model="form[item.code]"
                     v-if="item.type == 'select'"
          >
            <el-option v-for="(it,idx) in item.list" :key="idx"
                       :label="it[item.label]" :value="it[item.value]"
            />
          </el-select>

          <UploadImg v-model="form[item.code]" v-if="item.type == 'upload'"/>

        </el-form-item>
      </el-form>
    </div>

  </div>
</template>
<script setup lang="ts">
import UploadImg from "@/components/UploadFile/src/UploadImg.vue"
import Icon from "@/components/Icon/src/Icon.vue";
import {listToMap, formError, deepCopy} from "@/utils";

import {DICT_TYPE, getIntDictOptions, getStrDictOptions} from "@/utils/dict";

const flowType = getStrDictOptions(DICT_TYPE.FLOW_TYPE);
const payType = getStrDictOptions(DICT_TYPE.PAY_TYPE);


import {OneThaliApi} from "@/api/system/tenant/flow";


const props = defineProps({
  info: {
    type: Object,
    required: false,
    default: () => {
    }
  }
})

const FormRef = ref(null);

const provinceData = ref([])

const config = ref([

  {
    name: "流量包名称",
    code: "thali",
    required: true,
  },
  {
    name: "流量包类型",
    type: "select",
    code: "flowType",
    list: flowType,
    label: "label",
    value: "value",
    required: true,

  },
  {
    name: "包含流量(GB)",
    code: "dataValue",
    type:"number"

  },
  {
    name: "购买金额",
    code: "amount",
    required: true,
  },
])

const rules = computed(() => {
  let map = listToMap(config.value, 'code');

  for (let i in map) {
    if (map[i].required) {
      if (map[i].type == 'select' || map[i].type == 'upload') {
        map[i] = [{required: true, message: `请选择${map[i].name}`, trigger: 'change'}]
      } else {
        map[i] = [{required: true, message: `请输入${map[i].name}`, trigger: 'blur'}]
      }
    }

  }
  return map
})


const form = ref({})

const confirm = async (callback) => {

  try {
    let res = await FormRef.value.validate();

    console.log(res)

    let option = {
      ...form.value,
      dataValue:form.value.dataValue * 1024*1024,
      amount:Math.round(form.value.amount*100),
      type:payType.find(at=>at.label == '流量')?.value
    }


    if (form.value.id) {
      await OneThaliApi.updateOneThali(option);
    } else {
      await OneThaliApi.createOneThali(option);
    }
    callback && callback()
  } catch (e) {
    console.log(e,"eee")
    formError()
  }

}


/**
 * 生成省市区树
 * */
const generateProvince = (provinceData) => {
  console.log(provinceData)
  let cityList = [];
  provinceData.globeStateDetailList.forEach(item => {
    let isIn = cityList.find(at => {
      return item.province == at.name
    })
    if (!isIn) {
      cityList.push({
        name: item.province,
        list: []
      })
    }
  })

  cityList.forEach(item => {
    let lis = provinceData.globeStateDetailList.filter(it => {
      return it.province == item.name
    })

    item.list = (lis || []).map(item => {
      return {
        name: item.city,
        list: []
      }
    });

    item.list.forEach(at => {
      let lisArea = provinceData.globeStateDetailList.filter(it => {
        return it.city == at.name
      })

      at.list = (lisArea || []).map(z => {
        return {
          name: z.area,
          id: z.id
        }
      })
    })
  })

  return cityList
}



onMounted(() => {
})

defineExpose({
  confirm
})

watch(() => props.info, () => {
  form.value = props.info;
}, {
  deep: true,
  immediate: true
})
</script>
<style lang="scss" scoped>

</style>
